#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
    int h, n;
    cin >> h >> n;
    vector<int> nums(n);
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }
    sort(nums.begin(), nums.end(), [h](int a, int b) {
        if (abs(a - h) == abs(b - h)) {
            return a < b;
        }
        return abs(a - h) < abs(b - h);
    });
    for (int i = 0; i < n; i++) {
        cout << nums[i] << " ";
    }
    cout << endl;
    return 0;
}